package com.tinycloud.miracll.finance.service.impl;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.ArrayList;

import java.util.List;
import java.util.Map;


import com.tinycloud.common.core.text.UUID;
import com.tinycloud.common.core.utils.DateUtils;
import com.tinycloud.common.datasource.annotation.Master;

import com.tinycloud.miracll.finance.service.IERPProductCostService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tinycloud.miracll.finance.mapper.ProductCostMapper;
import com.tinycloud.miracll.finance.domain.ProductCost;
import com.tinycloud.miracll.finance.service.IProductCostService;

/**
 * 产品成本Service业务层处理
 * 将计算的结果成本同步到erp
 * @author tiny
 * @date 2021-08-31
 */
@Service
@Master
public class ProductCostServiceImpl implements IProductCostService 
{
    @Autowired
    private ProductCostMapper productCostMapper;

    @Autowired
    IERPProductCostService erpProductCostService;


    /**
     * 查询产品成本列表
     * 
     * @param productCost 产品成本
     * @return 产品成本
     */
    @Override
    public List<ProductCost> selectFirstProductCostList(ProductCost productCost)
    {
        return productCostMapper.selectFirstProductCostList(productCost);
    }

    @Override
    public List<ProductCost> selectSecondProductCostList(ProductCost productCost)
    {
        return productCostMapper.selectSecondProductCostList(productCost);
    }
    @Override
    public List<ProductCost> selectLastProductCostList(ProductCost productCost)
    {
        return productCostMapper.selectLastProductCostList(productCost);
    }
    /**
     * 新增产品成本
     * 
     * @param productCost 产品成本
     * @return 结果
     */
    @Override
    public int insertLastProductCost(ProductCost productCost)
    {
        productCost.setCreateTime(DateUtils.getNowDate());
        return productCostMapper.insertLastProductCost(productCost);
    }

    @Override
    public int insertFirstProductCost(ProductCost productCost)
    {
        productCost.setCreateTime(DateUtils.getNowDate());
        return productCostMapper.insertFirstProductCost(productCost);
    }

    @Override
    public int insertSecondProductCost(ProductCost productCost)
    {
        productCost.setCreateTime(DateUtils.getNowDate());
        return productCostMapper.insertSecondProductCost(productCost);
    }



    /**
     * 删除产品成本信息
     *
     * @param productCost 产品成本参数
     * @return 结果
     */
    @Override
    public void deleteLastProductCostByParams(ProductCost productCost) {

        productCostMapper.deleteLastProductCostByParams(productCost);
    }
    @Override
    public void deleteFirstProductCostByParams(ProductCost productCost) {

        productCostMapper.deleteFirstProductCostByParams(productCost);
    }

    @Override
    public void deleteSecondProductCostByParams(ProductCost productCost) {

        productCostMapper.deleteSecondProductCostByParams(productCost);
    }







    @Override
    public void firstProductCost() throws ParseException {

        List<ProductCost> productCostList = new ArrayList<>();
        List<Map<String,Object>>  productCostMapList =  productCostMapper.firstProductCost();
        System.out.println(productCostMapList.size());
        for (Map<String,Object> productCostTem : productCostMapList) {
            ProductCost productCost = new ProductCost();
            productCost.setProductCode(productCostTem.get("ItemCode").toString());
            productCost.setProductDesc(productCostTem.get("ItemName").toString());
            productCost.setMaterialCode(productCostTem.get("MaterialCode").toString());
            productCost.setMaterialDesc(productCostTem.get("Dscription").toString());

            productCost.setMaterialcount((BigDecimal) productCostTem.get("MaterialCount"));
            productCost.setTotalprice((BigDecimal) productCostTem.get("MaterialTotalPrice"));
            productCost.setOutput((BigDecimal) productCostTem.get("OutPut"));
            productCost.setBom((BigDecimal) productCostTem.get("bom"));
            productCost.setPrice((BigDecimal) productCostTem.get("price"));
            productCostList.add(productCost);

            productCost.setId(UUID.randomUUID().toString());
            productCost.setCostDate(DateUtils.parseDate(DateUtils.getDate(), DateUtils.YYYY_MM_DD));
            productCost.setCreateTime(DateUtils.getNowDate());
            productCost.setDelFlag("1");
            this.insertFirstProductCost(productCost);
        }
    }


    @Override
    public void firstProductCostList() throws ParseException {

        List<ProductCost> productCostList = new ArrayList<>();
        List<Map<String,Object>>  productCostMapList =  productCostMapper.firstProductCost();
        System.out.println(productCostMapList.size());
        for (Map<String,Object> productCostTem : productCostMapList) {
            ProductCost productCost = new ProductCost();
            productCost.setProductCode(productCostTem.get("ItemCode").toString());
            productCost.setProductDesc(productCostTem.get("ItemName").toString());
            productCost.setMaterialCode(productCostTem.get("MaterialCode").toString());
            productCost.setMaterialDesc(productCostTem.get("Dscription").toString());

            productCost.setMaterialcount((BigDecimal) productCostTem.get("MaterialCount"));
            productCost.setTotalprice((BigDecimal) productCostTem.get("MaterialTotalPrice"));
            productCost.setOutput((BigDecimal) productCostTem.get("OutPut"));
            productCost.setBom((BigDecimal) productCostTem.get("bom"));
            productCost.setPrice((BigDecimal) productCostTem.get("price"));
            productCostList.add(productCost);

            productCost.setId(UUID.randomUUID().toString());
            productCost.setCostDate(DateUtils.parseDate(DateUtils.getDate(), DateUtils.YYYY_MM_DD));
            productCost.setCreateTime(DateUtils.getNowDate());
            productCost.setDelFlag("1");
            this.insertFirstProductCost(productCost);
        }
    }

    @Override
    public void secondProductCostList() throws ParseException {

        ProductCost productCostParamTem = new ProductCost();
        productCostParamTem.setProductDesc("E001CYD2");
        List<ProductCost> productCostList = this.selectFirstProductCostList(null);
        for ( ProductCost productCost: productCostList ) {
                ProductCost productCostParam = new ProductCost();
                productCostParam.setProductCode(productCost.getMaterialCode());
                List<ProductCost> productCostListResult = this.selectFirstProductCostList(productCostParam);
                if (null==productCostListResult||0==productCostListResult.size()) {
                    this.insertSecondProductCost(productCost);
                }else {
                    String productCode = productCost.getProductCode();
                    String productDesc = productCost.getProductDesc();
                    BigDecimal output = productCost.getOutput();
                    BigDecimal materialcount =  productCost.getMaterialcount();

                    for ( ProductCost productCostTem: productCostListResult) {
                        BigDecimal temBOM = productCostTem.getBom();
                        BigDecimal temPrice = productCostTem.getPrice();
                        ProductCost productCostNew = new ProductCost();
                        productCostNew.setId(UUID.randomUUID().toString());
                        productCostNew.setProductCode(productCode);
                        productCostNew.setProductDesc(productDesc);
                        productCostNew.setMaterialCode(productCostTem.getMaterialCode());
                        productCostNew.setMaterialDesc(productCostTem.getMaterialDesc());
                        productCostNew.setOutput(output);
                        productCostNew.setMaterialcount(materialcount.multiply(temBOM));
                        productCostNew.setTotalprice(materialcount.multiply(temPrice));
                        productCostNew.setBom(productCostNew.getMaterialcount().divide(output,6,BigDecimal.ROUND_HALF_UP));
                        productCostNew.setPrice(productCostNew.getTotalprice().divide(output,6,BigDecimal.ROUND_HALF_UP));
                        productCostNew.setCostDate(DateUtils.parseDate(DateUtils.getDate(), DateUtils.YYYY_MM_DD));
                        productCostNew.setCreateTime(DateUtils.getNowDate());
                        productCost.setDelFlag("1");
                        this.insertSecondProductCost(productCostNew);

                    }

                }


        }

    }

    @Override
    public void lastProductCostList() throws ParseException {
        ProductCost productCostParamTem = new ProductCost();
        productCostParamTem.setProductDesc("E001CYD2");
        List<ProductCost> productCostList = this.selectSecondProductCostList(null);
        for ( ProductCost productCost: productCostList ) {
            ProductCost productCostParam = new ProductCost();
            productCostParam.setProductCode(productCost.getMaterialCode());
            List<ProductCost> productCostListResult = this.selectSecondProductCostList(productCostParam);
            if (null==productCostListResult||0==productCostListResult.size()) {
                this.insertLastProductCost(productCost);
            }else {
                String productCode = productCost.getProductCode();
                String productDesc = productCost.getProductDesc();
                BigDecimal output = productCost.getOutput();
                BigDecimal materialcount =  productCost.getMaterialcount();

                for ( ProductCost productCostTem: productCostListResult) {
                    BigDecimal temBOM = productCostTem.getBom();
                    BigDecimal temPrice = productCostTem.getPrice();
                    ProductCost productCostNew = new ProductCost();
                    productCostNew.setId(UUID.randomUUID().toString());
                    productCostNew.setProductCode(productCode);
                    productCostNew.setProductDesc(productDesc);
                    productCostNew.setMaterialCode(productCostTem.getMaterialCode());
                    productCostNew.setMaterialDesc(productCostTem.getMaterialDesc());
                    productCostNew.setOutput(output);
                    productCostNew.setMaterialcount(materialcount.multiply(temBOM));
                    productCostNew.setTotalprice(materialcount.multiply(temPrice));
                    productCostNew.setBom(productCostNew.getMaterialcount().divide(output,6,BigDecimal.ROUND_HALF_UP));
                    productCostNew.setPrice(productCostNew.getTotalprice().divide(output,6,BigDecimal.ROUND_HALF_UP));
                    productCostNew.setCostDate(DateUtils.parseDate(DateUtils.getDate(), DateUtils.YYYY_MM_DD));
                    productCostNew.setCreateTime(DateUtils.getNowDate());
                    productCost.setDelFlag("1");
                    this.insertLastProductCost(productCostNew);

                }

            }


        }

    }


    @Override
    public void insertERPProductCost() throws ParseException {
        List<ProductCost> productCostList = this.selectLastProductCostList(null);
        for (ProductCost productCost : productCostList) {
            erpProductCostService.insertProductCost(productCost);
        }


    }

    @Override
    public void deleteERPProductCostByParams(ProductCost productCost) {
        erpProductCostService.deleteLastProductCostByParams(productCost);
    }





}
